package com.jash.cardviewdemo;

import android.animation.ObjectAnimator;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener {

    private CardView card1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CardView card2 = (CardView) findViewById(R.id.card_2);
        card2.setOnTouchListener(this);
        card2.setOnClickListener(this);
        card1 = (CardView) findViewById(R.id.card_1);
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        CardView card = (CardView) view;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                card.setCardElevation(getResources().getDimension(R.dimen.pass_card_elevation));
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                card.setCardElevation(getResources().getDimension(R.dimen.card_elevation));
                break;
        }
        return !view.isClickable();
    }

    @Override
    public void onClick(View view) {
//        Toast.makeText(this, "点击事件", Toast.LENGTH_SHORT).show();
        int i = card1.getWidth() / 2;
        ViewCompat.setScaleX(card1, 0);
        ViewCompat.setScaleY(card1, 0);
        card1.setRadius(i);
        ViewCompat.animate(card1).scaleY(1).scaleX(1).setDuration(2000).start();
        ObjectAnimator radius = ObjectAnimator.ofFloat(card1, "radius", i, 0);
        radius.setStartDelay(2000);
        radius.setDuration(2000).start();
    }
}
